terraform {
  required_providers {
    huaweicloud = {
      source  = "local-registry/huaweicloud/huaweicloud"
      version = " 1.42.0"
    }
  }
}

# Configure the HuaweiCloud Provider
provider "huaweicloud" {
  region     = var.region_operation
  access_key = var.access_key
  secret_key = var.secret_key
}

provider "huaweicloud" {
  alias  = "close_to_resource"
 
  region     = var.region_resource
  access_key = var.access_key
  secret_key = var.secret_key
}

# Acquire the data 
data "huaweicloud_images_image" "myimage" {
  os        = var.OS_type
  name      = var.OS_type =="Windows" ? "Windows Server 2019 Datacenter 64bit" :"CentOS 7.9 64bit"
  most_recent = true
}

data "huaweicloud_images_image" "myimage_resources" {
  provider = huaweicloud.close_to_resource
  name     = var.OS_type =="Windows" ? "Windows Server 2019 Datacenter 64bit":"CentOS 7.9 64bit"
  most_recent = true
}

data "huaweicloud_identity_projects" "cc_instance_operator" {
  name = var.region_operation
}

data "huaweicloud_identity_projects" "cc_instance_resource" {
  name = var.region_resource
}



# Create resources in the region close to the operator (in ChinaMainland-Guangzhou)
# Create a VPC
resource "huaweicloud_vpc" "vpc" {
  name = "vpc-global-operation"
  cidr = "192.168.0.0/16"
}


# Create a subnet
resource "huaweicloud_vpc_subnet" "subnet1" {
  name       = "subnet-1"
  cidr       = "192.168.10.0/24"
  gateway_ip = "192.168.10.1"
  vpc_id     = huaweicloud_vpc.vpc.id
}


# Create secgroup and rule
resource "huaweicloud_networking_secgroup" "mysecgroup" {
  name                 = "sg-global-operation"
  description          = "The security group for global-operation"
  delete_default_rules = false
}

resource "huaweicloud_networking_secgroup_rule" "allow_ping" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "icmp"
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.mysecgroup.id
}

resource "huaweicloud_networking_secgroup_rule" "allow_remote_login" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = var.OS_type =="Windows" ? 3389 : 22
  port_range_max    = var.OS_type =="Windows" ? 3389 : 22
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.mysecgroup.id
}

# Create the jumper-ecs in ChinaMainland-Guangzhou
resource "huaweicloud_compute_instance" "ecs" {
  name               = "ecs-global-operation"
  image_id           = data.huaweicloud_images_image.myimage.id
  flavor_id          = var.OS_type =="Windows"? "s6.large.2" :"s6.medium.2"              #在此处可修改Windows和CentOS对应的ECS规格，“：”前为Windows使用的服务器，后为CentOS使用的服务器
  security_group_ids = [huaweicloud_networking_secgroup.mysecgroup.id]
  charging_mode      = var.ecs_charging_mode =="prePaid"?"prePaid":"postPaid"
  period_unit        = var.ecs_charging_unit
  period             = var.ecs_charging_period
  admin_pass         = var.ecs_operation_admin_pass
  system_disk_type   = "GPSSD"
  auto_pay           = "true"


  network {
    uuid = huaweicloud_vpc_subnet.subnet1.id
  }
}

# Create the eip of the ecs
resource "huaweicloud_vpc_eip" "myeip" {
  publicip {
    type = var.bgp_and_region[var.region_resource]
  }
  bandwidth {
    name        = "eip-global-operation"
    size        = 5
    share_type  = "PER"
    charge_mode = var.eip_charging_mode =="bandwidth" ? "bandwidth" : "traffic" 
  }
}

# Associated the eip and the ecs
resource "huaweicloud_compute_eip_associate" "associated" {
  public_ip   = huaweicloud_vpc_eip.myeip.address
  instance_id = huaweicloud_compute_instance.ecs.id
}



# Create resources oversea (in HongKong)
# Create a VPC
resource "huaweicloud_vpc" "vpc_resources" {
  provider = huaweicloud.close_to_resource
  name = "vpc-global-operation"
  cidr = "172.16.0.0/16"
}

# Create a subnet
resource "huaweicloud_vpc_subnet" "subnet2" {
  provider   =  huaweicloud.close_to_resource
  name       = "subnet-1"
  cidr       = "172.16.10.0/24"
  gateway_ip = "172.16.10.1"
  vpc_id     = huaweicloud_vpc.vpc_resources.id
}

# Create secgroup and rule
resource "huaweicloud_networking_secgroup" "mysecgroup_resources" {
  provider             =  huaweicloud.close_to_resource
  name                 = "sg-global-operation-resources"
  description          = "The security group for global-operation"
  delete_default_rules = false
}

resource "huaweicloud_networking_secgroup_rule" "allow_ping_resources" {
  provider          =  huaweicloud.close_to_resource
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "icmp"
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.mysecgroup_resources.id
}

resource "huaweicloud_networking_secgroup_rule" "allow_remote_login_resources" {
  provider          =  huaweicloud.close_to_resource
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = var.OS_type =="Windows" ? 3389 : 22
  port_range_max    = var.OS_type =="Windows" ? 3389 : 22
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = huaweicloud_networking_secgroup.mysecgroup_resources.id
}

# Create the Jumper-ecs in HongKong
resource "huaweicloud_compute_instance" "ecs_resources" {
  provider           = huaweicloud.close_to_resource
  name               = "ecs-global-operation"
  image_id           = data.huaweicloud_images_image.myimage_resources.id
  flavor_id          = var.OS_type =="Windows"? "s6.large.2" :"s6.medium.2"                #在此处可修改Windows和CentOS对应的ECS规格，“：”前为Windows使用的服务器，后为CentOS使用的服务器
  security_group_ids = [huaweicloud_networking_secgroup.mysecgroup_resources.id]
  charging_mode      = var.ecs_charging_mode =="prePaid"?"prePaid":"postPaid"
  period_unit        = var.ecs_charging_unit
  period             = var.ecs_charging_period  
  
  system_disk_type   = "GPSSD"
  admin_pass         = var.ecs_resource_admin_pass
  auto_pay           = "true"

  network {
    uuid = huaweicloud_vpc_subnet.subnet2.id
  }
}

# Create the eip of the ecs
resource "huaweicloud_vpc_eip" "myeip_resources" {
  provider = huaweicloud.close_to_resource 
  publicip {
    type = var.bgp_and_region[var.region_resource]
  }
  bandwidth {
    name        = "eip-global-operation_resources"
    size        = 5
    share_type  = "PER"
    charge_mode =  var.eip_charging_mode =="bandwidth" ? "bandwidth" : "traffic" 
  }
}

# Associated the eip and the ecs
resource "huaweicloud_compute_eip_associate" "associate_resources" {
  provider    = huaweicloud.close_to_resource   
  public_ip   = huaweicloud_vpc_eip.myeip_resources.address
  instance_id = huaweicloud_compute_instance.ecs_resources.id
}

# Create CloudConnetction instance(CC)
resource "huaweicloud_cc_connection""global_operation_cc"{
  name   =  "cc_global_operation"
  enterprise_project_id  =  "0"
}

# Create network_instance of CC based on the VPC in ChinaMainland-Guangzhou
resource "huaweicloud_cc_network_instance" "cc_instance1" {
  type                = "vpc"
  cloud_connection_id = huaweicloud_cc_connection.global_operation_cc.id
  instance_id         = huaweicloud_vpc.vpc.id
  project_id          = data.huaweicloud_identity_projects.cc_instance_operator.projects[0].id
  region_id           = var.region_operation
  cidrs = [
    huaweicloud_vpc_subnet.subnet1.cidr
  ]
}

# Create network_instance of CC based on the VPC oversea(in Hongkong)
resource "huaweicloud_cc_network_instance" "cc_instance2" {
  type                = "vpc"
  cloud_connection_id = huaweicloud_cc_connection.global_operation_cc.id
  instance_id         = huaweicloud_vpc.vpc_resources.id
  project_id          = data.huaweicloud_identity_projects.cc_instance_resource.projects[0].id
  region_id           = var.region_resource
  cidrs = [
    huaweicloud_vpc_subnet.subnet2.cidr
  ]
}